/**
 * ANIMATIONS BLENDING and COMPONENTS.
 */
package  
{
	import flare.basic.*;
	import flare.core.*;
	import flare.loaders.collada.*;
	import flare.system.*;
	import flare.utils.*;
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import components.*;
	import net.jpauclair.FlashPreloadProfiler;
	
	[SWF(frameRate = 120, backgroundColor = 0x808080, width = 800, height = 600 )]
	/**
	 * ...
	 * @author Ariel Nehamd
	 */
	public class Test08_Anim_Blending extends Sprite
	{
		private var scene:Scene3D;		
		private var model:Pivot3D;
		private var skin:Pivot3D;
		private var walk:Label3D;
		private var run:Label3D;
		
		public function Test08_Anim_Blending() 
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			//addChild( new FlashPreloadProfiler() );
			
			scene = new Viewer3D( this );
			scene.addEventListener( Scene3D.COMPLETE_EVENT, completeEvent );
			
			model = scene.addChildFromFile( "player.f3d" );
			model.setScale( 70, 70, 70 );
			
			// camera posiion and orientation.
			scene.camera.setPosition( 120, 120, -120 );
			scene.camera.lookAt( 0, 50, 0 );
			
			// light parameters.
			scene.ambientColor = new Vector3D( 0.2, 0.2, 0.2 );  // <-- RGB ambient light color.
			scene.defaultLight.color = new Vector3D( 1, 1, 1, 1 ); // <-- RGB light colors and contrast.
			scene.defaultLight.setRotation( 40, 40, 0 );	
		}
		
		private function completeEvent(e:Event):void 
		{
			// this component search for a skinned mesh 
			// and draw the bones as lines.
			model.addComponent( new DrawBones() );
			
			// define some labels.
			model.addLabel( new Label3D( "walk", 0, 41 ) );
			model.addLabel( new Label3D( "run", 50, 74 ) );
			
			scene.playLabel( "walk" ); 
			
			scene.addEventListener( Scene3D.UPDATE_EVENT, updateEvent );
		}
		
		private function updateEvent(e:Event):void 
		{
			// blend between the animations.
			if ( Input3D.keyHit( Input3D.S ) ) model.stop();
			if ( Input3D.keyHit( Input3D.NUMBER_1 ) ) model.playLabel( "walk", true, 15 );
			if ( Input3D.keyHit( Input3D.NUMBER_2 ) ) model.playLabel( "run", true, 15 );
		}		
	}
}